package softswitch.service;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import softswitch.model.Country;
import softswitch.model.Route;
import softswitch.model.RouteGroup;
import softswitch.model.Operator;

@Stateless
public class RouteService {

	
	@PersistenceContext(unitName = "persistence")
    private EntityManager em;
	
	
	public Route getById(Integer id){	
		return em.find(Route.class, id);
	}
	
	public Route save(Route route) {
    	em.persist(route); 
    	return route;
    }
	
	public Route update(Route route) {
    	return em.merge(route);
    }
    
    public void delete(Integer id) {
    	em.remove(em.find(Route.class, id));	
    }
	
    @SuppressWarnings("unchecked")
    public List<Route> list(int first, int max) {    	
		Query query = em.createQuery("SELECT e FROM Route e");
    	if (max > 0) {
            query.setMaxResults(max);
        }
        if (first > 0) {
            query.setFirstResult(first);
        }
        return query.getResultList();   
    }
	
	//---------------------------------------------------------------------------------------
    
	@SuppressWarnings("unchecked")
	public List<Route> listByRouteGroup(RouteGroup routegroup) {
    	List<Route> results = null;
    	Query query = em.createQuery("SELECT e FROM Route e WHERE e.routegroup=:routegroup");
		query.setParameter("routegroup", routegroup);
		try {
			results = query.getResultList(); 
        }
    	catch (Exception e) {
			e.printStackTrace();
		}
        return results; 
	}
	
	@SuppressWarnings("unchecked")
	public List<Route> listByCountry(Country country) {
    	List<Route> results = null;
    	Query query = em.createQuery("SELECT e FROM Route e WHERE e.country=:country");
		query.setParameter("country", country);
		try {
			results = query.getResultList(); 
        }
		catch (Exception e) {
			e.printStackTrace();
		}
        return results;
    }

	
    @SuppressWarnings("unchecked")
	public List<Route> listByCountryRouteGrupOperator(Country country, RouteGroup routegroup, Operator operator) {
    	List<Route> results = null;
    	
    	String sql = "SELECT e FROM Route e ";
    	Boolean first = true;
        
        if(country!=null){    	
        	sql = sql + "WHERE e.country=:country";
        	first = false;
        }
        if(routegroup!=null){
        	if(first){
        		sql = sql + "WHERE e.routegroup=:routegroup";
        		first = false;
        	}
        	else{
        		sql = sql + "AND e.routegroup=:routegroup";
        	}
        }
        if(operator!=null){
        	if(first){
        		sql = sql + "WHERE e.operator=:operator";
        		first = false;
        	}
        	else{
        		sql = sql + "AND e.operator=:operator";
        	}
        }        	
        
        Query query = em.createQuery(sql);
        if(country!=null){    	
        	query.setParameter("country", country);
        }
        if(routegroup!=null){
        	query.setParameter("routegroup", routegroup);
        }
        if(operator!=null){
        	query.setParameter("operator", operator);
        }
        
        results = query.getResultList();
        return results;

    }
	
    public Route getByNameCountryRouteGrupOperator(String name, Country country, RouteGroup routegroup, Operator operator) {
        Route result = null;
        Query queryCount = em.createQuery("SELECT COUNT(e) FROM Route e WHERE e.country=:country AND e.name =:name AND e.routegroup =:routegroup AND e.operator = :operator");
        queryCount.setParameter("name", name);
        queryCount.setParameter("country", country);
        queryCount.setParameter("routegroup", routegroup);
        queryCount.setParameter("operator", operator);
        Query query = em.createQuery("SELECT e FROM Route e WHERE e.country=:country AND e.name =:name AND e.routegroup =:routegroup AND e.operator = :operator");
        query.setParameter("name", name);
        query.setParameter("country", country);
        query.setParameter("routegroup", routegroup);
        query.setParameter("operator", operator);
        query.setFirstResult(0);
        query.setMaxResults(1);

        if ((Long) queryCount.getSingleResult() != 0) {
            try {

                result = (Route) query.getSingleResult();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return result;

    }
}

